package ngames.com.sushitv.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import ngames.com.sushitv.R;
import ngames.com.sushitv.base.BaseListAdapter;
import ngames.com.sushitv.entity.FanMadeContent;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.interfaces.OnItemClickListener;

/**
 * Created by Fearless on 2016/6/26.
 */
public class FanMadeContentAdapter extends BaseListAdapter<FanMadeContent> {
    private View view;

    public FanMadeContentAdapter(Context context, View view, ArrayList<FanMadeContent> list) {
        super(context, list);
        this.view = view;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VideoRankHolder(mInflater.inflate(R.layout.item_fanmade_video_6p, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VideoRankHolder videoRankHolder = (VideoRankHolder) holder;
        videoRankHolder.setData(position);
    }


    class VideoRankHolder extends RecyclerView.ViewHolder {
        TextView nameTxt;
        RecyclerView mRecycler;
        VideoRankAdapter mAdapter;
        String mDataID = "0";
        public VideoRankHolder(View itemView) {
            super(itemView);
            nameTxt = (TextView) itemView.findViewById(R.id.text_fanMade_name);

            mRecycler = (RecyclerView) itemView.findViewById(R.id.recycle_fanMade_item);
            mRecycler.setHasFixedSize(true);
            mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
            /*mLinearLayoutManager = new LinearLayoutManager((Activity)mContext);
            mRecycler.setLayoutManager(mLinearLayoutManager);*/
        }

        public void setData(int position) {
            FanMadeContent content = getItem(position);
            nameTxt.setText("Featured 【"+content.getName()+"】");
            ArrayList<ItemVideo> data = content.getItemVideos();
            if ("0".equals(mDataID) && mAdapter != null) {
                mAdapter.clearData();
            }
            if (data.isEmpty()) {
                return;
            }
            mDataID = data.get(data.size() - 1).getId();
            setListData(data);
        }

        public void setListData(ArrayList<ItemVideo> data) {

            if (mAdapter == null) {
                mAdapter = new VideoRankAdapter(mContext, data);
                mAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                    }

                    @Override
                    public void onItemButtonClick(View view, int position) {

                    }
                });
                mRecycler.setAdapter(mAdapter);
            } else {
                mAdapter.addData(data);
            }

        }
    }
}
